import Services from './services'
import reqData from '../server/utils/reqData'

export default {
  async nuxtServerInit ({ commit, state }, { req, res }) {
    console.log('nuxtServerInit')
    console.log(req.headers.cookie)
    if (req.headers.cookie && reqData.getResCookie(req.headers.cookie, 'openid')) {
      var datas = {openId: reqData.getResCookie(req.headers.cookie, 'openid')}
      const getUseridByopenid = await Services.getUserIdByOpenId(datas, req)
      if (getUseridByopenid && getUseridByopenid.body && getUseridByopenid.body.responseCode === 0) {
        commit('SET_USERHASLOGIN', true)
      } else {
        console.log(getUseridByopenid.body.responseMessage)
      }
      // commit('SET_OPENID', reqData.getResCookie(req.headers.cookie, 'openid'))
    } else if (req.query.code) {
      const wechatInfo = await Services.getWeChatOpenid(req.query.code, req)
      var domain = (req.hostname.indexOf('hairongyi.com') < 0) ? req.hostname : '.hairongyi.com'
      res.cookie('wechatInfo', wechatInfo.data, {domain: domain})
      if (wechatInfo && wechatInfo.data && wechatInfo.data.openid) {
        var datas = {openId: wechatInfo.data.openid}
        const getUseridByopenid = await Services.getUserIdByOpenId(datas)
        if (getUseridByopenid && getUseridByopenid.body && getUseridByopenid.body.userId) {
          commit('SET_USERHASLOGIN', true)
        } else {
          console.log(getUseridByopenid.body.responseMessage)
        }
        var domain = (req.hostname.indexOf('hairongyi.com') < 0) ? req.hostname : '.hairongyi.com'
        res.cookie('openid', wechatInfo.data.openid, {domain: domain})
      }
    } else if (req.headers.cookie && reqData.getResCookie(req.headers.cookie, 'com.vfsso.cas.token')) {
      var token = reqData.getResCookie(req.headers.cookie, 'com.vfsso.cas.token')
      var userInfo = await Services.getUserIdBytoken(token, req)
      console.log(userInfo.body)
      if (userInfo && userInfo.body && userInfo.body.userDto && userInfo.body.userDto.userId) {
        commit('SET_USERHASLOGIN', true)
        console.log(state.userInfo)
      }
    }
  },

  async setUserLogin ({commit, state}, boolean) {
    await commit('SET_USERHASLOGIN', boolean)
    if (boolean === false){
      await commit('SET_USERINVEST', {})
      await commit('SET_REALNAME', "")
      await commit('SET_SYZLIST', [])
      await commit('SET_TBZLIST', [])
      await commit('SET_YJQLIST', [])
      await commit('SET_SYZQUERY', false)
      await commit('SET_TBZQUERY', false)
      await commit('SET_YJQQUERY', false)
    }
    return true
  },

  setInvestList ({ commit, state }, datas) {
    var obj = {
      'HKZ': {
        'LIST': 'SET_SYZLIST',
        'QUERY': 'SET_SYZQUERY'
      },
      'TBZ': {
        'LIST': 'SET_TBZLIST',
        'QUERY': 'SET_TBZQUERY'
      },
      'YJQ': {
        'LIST': 'SET_YJQLIST',
        'QUERY': 'SET_YJQQUERY'
      }
    }
    commit(obj[datas.status]['QUERY'], true)
    commit(obj[datas.status]['LIST'], datas.investList)
  },

  setrealName ({ commit, state }, realName) {
    commit('SET_REALNAME', realName)
  },

  setUserInvest ({ commit, state }, userInvest) {
    commit('SET_USERINVEST', userInvest)
  }

}
